﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Streambolics.Hp41s.Gui
{
    /// <summary>
    ///     Encapsulation of the Drag-and-drop data that
    ///     can be exchanged between HP-41S controls.
    /// </summary>

    public class DragData
    {
        private Data _Data;
        private Control _Source;

        public DragData (Data aData, Control aSource)
        {
            _Data = aData;
            _Source = aSource;
        }

        public Data Data
        {
            get
            {
                return _Data;
            }
        }

        public Control Source
        {
            get
            {
                return _Source;
            }
        }

        /// <summary>
        ///     Extract dragged data from a Drag&Drop event
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the expected data.
        /// </typeparam>
        /// <param name="aDragData">
        ///     The event from which the data should be extracted.
        /// </param>
        /// <returns>
        ///     The extracted data if <c>aDragData</c> contains data
        ///     of type <c>T</c>. Null otherwise.
        /// </returns>

        public static T GetDragData<T> (DragEventArgs aDragData, Control aTarget)
            where T : Data
        {
            if (aDragData.Data.GetDataPresent (typeof (DragData)))
            {
                DragData d = (DragData)aDragData.Data.GetData (typeof (DragData));
                if (d.Source != aTarget && d.Data is T)
                {
                    return (T)(d.Data);
                }
            }
            return default (T);
        }

    }
}
